package com.epam.training;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.epam.training.domain.Admin;
import com.epam.training.service.user.UserService;

@Controller
public class SaveAdminController {

	private final UserService userService;

	@Autowired
	public SaveAdminController(UserService userService) {
		this.userService = userService;
	}

	@ModelAttribute
	private Admin createAdmin(@RequestParam String aName,
			@RequestParam String aLogin, @RequestParam String aPassword) {
		Admin admin = new Admin();
		admin.setLogin(aLogin);
		admin.setPassword(aPassword);
		admin.setName(aName);

		return admin;
	}

	@RequestMapping("/admim/update/admin")
	private String updateAdmin(@ModelAttribute Admin admin) {
		userService.update(admin);

		return "/admin/show/admin";
	}
}
